﻿using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace PigLatinTranslator
{
    //Its job is to manage all the Sentence classes that will be translated
    //This could have been seperated into multiple classes, set methods instead of a collection
    //Then it would not have made the translate methods 2 lines
    public abstract class Translator
    {
        //Rule for seperating sentences
        const string SentenceSeperation = @"[^.?!]*[.?!]|[.?!]*[a-z\s'-]*$";

        private static Translator currentTranslator;

        private List<Sentence> sentences;

        public List<Sentence> Sentences
        {
            get
            {
                if (sentences == null)
                {
                    sentences = new List<Sentence>();
                }
                return sentences;
            }
        }

        //This methods job is to create the list of sentences
        private void CreateListOfSentences(string Input)
        {
            //Get a collection of all the matches which are sentences
            MatchCollection MatchSentences = Regex.Matches(Input, SentenceSeperation, RegexOptions.IgnoreCase);
            //Add a new Sentence to the list passing it the Match
            foreach (Match match in MatchSentences)
            {
                Sentences.Add(new Sentence(match));
            }
        }

        public abstract string translateWord(string word);

        public static string PigLatinToEnglish(string input)
        {
            currentTranslator = new PigLatinToEnglishTranslator();
            return currentTranslator.TranslateInput(input);
        }

        public static string EnglishToPigLatin(string input)
        {
            currentTranslator = new EnglishToPigLatinTranslator();
            return currentTranslator.TranslateInput(input);
        }

        //The main method which runs the translator
        public string TranslateInput(string Input)
        {
            CreateListOfSentences(Input);
            StringBuilder FinalTranslation = new StringBuilder();
            foreach (Sentence sentence in Sentences)
            {
                FinalTranslation.Append(sentence.TranslateSentence(this));
                FinalTranslation.Append("  ");
            }
            //Remove the extra spaces from last sentence
            FinalTranslation.Remove(FinalTranslation.Length - 2, 2);
            return FinalTranslation.ToString();
        }
    }

}
